package com.example.demo.controller;

import com.example.demo.common.FileUtil;
import com.example.demo.common.OkHttpUtils;
import com.example.demo.common.PropertyUtil;
import com.example.demo.common.SessionAndGetRemote;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.*;

@Controller
@RequestMapping(value = "goSHGL")
public class GoController {
    private static final Logger logger = LoggerFactory.getLogger(GoController.class);

    private static String BACKURL = PropertyUtil.getValue("backurl");
//    private static String BACKURL = "http://127.0.0.1:8067
//    private static String BACKURL = "http://localhost:8080/shglxtback";

    @Autowired
    private OkHttpUtils okHttpUtils;

    @Autowired
    private FileUtil fileUtil;


    /*####################################################################################*/
    /*##############################   登陆管理    #######################################*/
    /*####################################################################################*/

    @RequestMapping(value="/login",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> goLogin(@RequestBody Map<String,Object> param, HttpServletRequest request){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/login",headersParams,jsonObject.toString(),new Object());
        JSONObject resultJson = JSONObject.fromObject(statusMap.get("data"));
        Map json = (Map) JSONObject.fromObject(resultJson);
        HttpSession session = request.getSession();
        if(json.size()>1){
            String username = json.get("username")!=null?json.get("username").toString():null;
            String store = json.get("store")!=null?json.get("store").toString():null;
            String userno = json.get("userno")!=null?json.get("userno").toString():null;
            String bm = json.get("bm")!=null?json.get("bm").toString():null;
            session.setAttribute("userno",userno);
            session.setAttribute("username",username);
            session.setAttribute("store",store);
            session.setAttribute("bm",bm);
            session.setMaxInactiveInterval(60*60*4);
        }
        return json;
    }

    /**
         * @Description 初始化界面 查看是否存有session 并获取所有门店
         * @Author zhangxue
         * @Date 16:29 2018/7/11
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/initPage",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String initPage(HttpSession session){
        String username = session.getAttribute("username")!=null?session.getAttribute("username").toString():null;
        String Store = session.getAttribute("store")!=null?session.getAttribute("store").toString():null;
        String userno = session.getAttribute("userno")!=null?session.getAttribute("userno").toString():null;
        String bm = session.getAttribute("bm")!=null?session.getAttribute("bm").toString():null;
        Map<String,Object> tMap = new HashMap<>();
        tMap.put("username",username);
        tMap.put("store",Store);
        tMap.put("userno",userno);
        tMap.put("bm",bm);
        JSONObject js = new JSONObject();
        if(username==null||Store==null){
            tMap.put("status","0");
            js = JSONObject.fromObject(tMap);
            return js.toString();
        }else{
            JSONObject jsonObject = JSONObject.fromObject(tMap);
            Map<String,String> headersParams = new HashMap<String,String>();
            headersParams.put("Content-Type","application/json;UTF-8");
            Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/initPage",headersParams,jsonObject.toString(),new Object());
            statusMap.put("status","1");
            statusMap.put("store",Store);
            statusMap.put("username",username);
            statusMap.put("userno",userno);
            statusMap.put("bm",bm);
            js = JSONObject.fromObject(statusMap);
            return js.toString();
        }
    }

    /**
     * @Description 初始化sh_search.html界面 查看是否存有session 并获取所有门店
     * @Author zhangxue
     * @Date 16:29 2018/7/11
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/initSearchPage",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String initSearchPage(HttpSession session){
        String username = session.getAttribute("username")!=null?session.getAttribute("username").toString():null;
        String Store = session.getAttribute("store")!=null?session.getAttribute("store").toString():null;
        String userno = session.getAttribute("userno")!=null?session.getAttribute("userno").toString():null;
        String bm = session.getAttribute("bm")!=null?session.getAttribute("bm").toString():null;
        Map<String,Object> tMap = new HashMap<>();
        tMap.put("username",username);
        tMap.put("store",Store);
        tMap.put("userno",userno);
        tMap.put("bm",bm);
        JSONObject js = new JSONObject();
        if(username==null||Store==null){
            tMap.put("status","0");
            js = JSONObject.fromObject(tMap);
            return js.toString();
        }else{
            JSONObject jsonObject = JSONObject.fromObject(tMap);
            Map<String,String> headersParams = new HashMap<String,String>();
            headersParams.put("Content-Type","application/json;UTF-8");
            Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/initSearchPage",headersParams,jsonObject.toString(),new Object());
            statusMap.put("status","1");
            statusMap.put("store",Store);
            statusMap.put("username",username);
            statusMap.put("userno",userno);
            statusMap.put("bm",bm);
            js = JSONObject.fromObject(statusMap);
            return js.toString();
        }
    }


    /*####################################################################################*/
    /*##############################   商户管理    #######################################*/
    /*####################################################################################*/
    /**
     * @Description 通过品牌名称查询所有品牌系列
     * @Author zhangxue
     * @Date 12:45 2018/5/31
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryBrandListByBrand",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryBrandListByBrand(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/queryBrandListByBrand";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }


    /**
     * @Description 查询所有商户信息
     * @Author zhangxue
     * @Date 12:45 2018/5/31
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryAllSHInfos",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllSHInfos(@RequestBody Map<String,Object> param,HttpSession session){
        String username = session.getAttribute("username")!=null?session.getAttribute("username").toString():null;
        String Store = session.getAttribute("store")!=null?session.getAttribute("store").toString():null;
        String userno = session.getAttribute("userno")!=null?session.getAttribute("userno").toString():null;
        String bm = session.getAttribute("bm")!=null?session.getAttribute("bm").toString():null;
        if(!"总部".equals(Store)){
            param.put("Store",Store);
        }
        String url = BACKURL+"/queryAllSHInfos";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }


    /**
         * @Description 新增商户信息
         * @Author zhangxue
         * @Date 11:12 2018/7/12
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/saveSHInfo",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveSHInfo(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/saveSHInfo";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    @RequestMapping(value="/delSHInfo",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delSHInfo(@RequestBody Map<String,Object> param,HttpServletRequest request){
        String path = request.getSession().getServletContext().getRealPath("/")
                + "uploadfile/";
        String ID = param.get("ID")!=null?param.get("ID").toString():null;
        String FilePath = path+ID;
        File delFile = new File(FilePath);
        if(delFile.exists()){
            Boolean flag = fileUtil.deleteDirectory(FilePath);
            if(flag){
                System.out.print("商户编号为"+ID+"的商户下图片删除成功");
            }
        }
        String url = BACKURL+"/delSHInfo";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    @RequestMapping(value="/editSHInfo",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String editSHInfo(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/editSHInfo";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
         * @Description 查询不同类型图片
         * @Author zhangxue
         * @Date 9:11 2018/9/12
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryImgList",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryImgList(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/queryImgList";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
         * @Description 上传授权书图片
         * @Author zhangxue
         * @Date 18:08 2018/7/12
         * @Param
         * @retrun
         **/
    @ResponseBody
    @RequestMapping(value = "/uploadBook")
    public String uploadBook(@RequestParam(value = "file", required = true) MultipartFile[] file,@RequestParam(value="ShNo")String ShNo,@RequestParam(value="Type")String Type,HttpServletRequest request) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String oldName = "";
        String fname = "";
        String fpath = "";
        // 定义允许上传的文件扩展名

        try {
            //如果文件不为空，写入上传路径
            if(!file[0].isEmpty()) {
                //上传文件路径
                // 得到上传文件的扩展名
                String fileExt = file[0]
                        .getOriginalFilename()
                        .substring(
                                file[0].getOriginalFilename()
                                        .lastIndexOf(".") + 1)
                        .toLowerCase();
                String fileName = UUID.randomUUID().toString()
                        .replaceAll("-", "")
                        + "_" + ShNo + "." + fileExt;
                fpath = request.getScheme() + "://"
                        + request.getServerName() + ":"
                        + request.getServerPort()
                        + request.getContextPath() + "/"
                        + "uploadfile/"+ShNo+"/" +"sqs"+"/"+ fileName;
                String path = request.getSession().getServletContext().getRealPath("/")
                        + "uploadfile/"  + "/";
                //上传文件名
                oldName = file[0].getOriginalFilename();
                File filepath = new File(path+ShNo+"/"+"sqs"+"/"+fileName);
                //判断路径是否存在，如果不存在就创建一个
                if (!filepath.getParentFile().exists()) {
                    filepath.getParentFile().mkdirs();
                }
                //将上传文件保存到一个目标文件当中
                file[0].transferTo(new File(path +ShNo+"/"+ "sqs"+"/"+fileName));
                Map<String, Object> param = new HashMap<>();
                param.put("ShNo", ShNo);
                param.put("Type", Type);
                param.put("fileurl", fpath);
                param.put("newfilename", fileName);
                String url = BACKURL+"/uploadBook";
                Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
                String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
                return data;
            } else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Description 上传执照图片
     * @Author zhangxue
     * @Date 18:08 2018/7/12
     * @Param
     * @retrun
     **/
    @ResponseBody
    @RequestMapping(value = "/uploadLicense")
    public String uploadLicense(@RequestParam(value = "file", required = true) MultipartFile[] file,@RequestParam(value="ShNo")String ShNo,@RequestParam(value="Type")String Type,HttpServletRequest request) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String oldName = "";
        String fname = "";
        String fpath = "";
        try {
            if(!file[0].isEmpty()) {
                String fileExt = file[0]
                        .getOriginalFilename()
                        .substring(
                                file[0].getOriginalFilename()
                                        .lastIndexOf(".") + 1)
                        .toLowerCase();
                String fileName = UUID.randomUUID().toString()
                        .replaceAll("-", "")
                        + "_" + ShNo + "." + fileExt;
                fpath = request.getScheme() + "://"
                        + request.getServerName() + ":"
                        + request.getServerPort()
                        + request.getContextPath() + "/"
                        + "uploadfile/"+ShNo+"/"+"zz"+"/" + fileName;
                String path = request.getSession().getServletContext().getRealPath("/")
                        + "uploadfile/"  + "/";
                //上传文件名
                oldName = file[0].getOriginalFilename();
                File filepath = new File(path+ShNo+"/"+"zz"+"/" +fileName);
                //判断路径是否存在，如果不存在就创建一个
                if (!filepath.getParentFile().exists()) {
                    filepath.getParentFile().mkdirs();
                }
                //将上传文件保存到一个目标文件当中
                file[0].transferTo(new File(path +ShNo+"/"+"zz"+"/" + fileName));
                Map<String, Object> param = new HashMap<>();
                param.put("ShNo", ShNo);
                param.put("fileurl", fpath);
                param.put("Type", Type);
                param.put("newfilename", fileName);
                String url = BACKURL+"/uploadLicense";
                Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
                String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
                return data;
            } else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Description 上传身份证图片
     * @Author zhangxue
     * @Date 18:08 2018/7/12
     * @Param
     * @retrun
     **/
    @ResponseBody
    @RequestMapping(value = "/uploadCard")
    public String uploadCard(@RequestParam(value = "file", required = true) MultipartFile[] file,@RequestParam(value="ShNo")String ShNo,@RequestParam(value="Type")String Type,HttpServletRequest request) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String oldName = "";
        String fname = "";
        String fpath = "";
        try {
            if(!file[0].isEmpty()) {
                String fileExt = file[0]
                        .getOriginalFilename()
                        .substring(
                                file[0].getOriginalFilename()
                                        .lastIndexOf(".") + 1)
                        .toLowerCase();
                String fileName = UUID.randomUUID().toString()
                        .replaceAll("-", "")
                        + "_" + ShNo + "." + fileExt;
                fpath = request.getScheme() + "://"
                        + request.getServerName() + ":"
                        + request.getServerPort()
                        + request.getContextPath() + "/"
                        + "uploadfile/"+ShNo+"/"+"card"+"/" + fileName;
                String path = request.getSession().getServletContext().getRealPath("/")
                        + "uploadfile/"  + "/";
                //上传文件名
                oldName = file[0].getOriginalFilename();
                File filepath = new File(path+ShNo+"/"+"card"+"/" +fileName);
                //判断路径是否存在，如果不存在就创建一个
                if (!filepath.getParentFile().exists()) {
                    filepath.getParentFile().mkdirs();
                }
                //将上传文件保存到一个目标文件当中
                file[0].transferTo(new File(path +ShNo+"/"+"card"+"/" + fileName));
                Map<String, Object> param = new HashMap<>();
                param.put("ShNo", ShNo);
                param.put("Type", Type);
                param.put("fileurl", fpath);
                param.put("newfilename", fileName);
                String url = BACKURL+"/uploadCard";
                Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
                String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
                return data;
            } else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Description 上传质检报告图片
     * @Author zhangxue
     * @Date 18:08 2018/7/12
     * @Param
     * @retrun
     **/
    @ResponseBody
    @RequestMapping(value = "/uploadZjReport")
    public String uploadZjReport(@RequestParam(value = "file", required = true) MultipartFile[] file,@RequestParam(value="ShNo")String ShNo,@RequestParam(value="Type")String Type,HttpServletRequest request) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String oldName = "";
        String fname = "";
        String fpath = "";
        try {
            if(!file[0].isEmpty()) {
                String fileExt = file[0]
                        .getOriginalFilename()
                        .substring(
                                file[0].getOriginalFilename()
                                        .lastIndexOf(".") + 1)
                        .toLowerCase();
                String fileName = UUID.randomUUID().toString()
                        .replaceAll("-", "")
                        + "_" + ShNo + "." + fileExt;
                fpath = request.getScheme() + "://"
                        + request.getServerName() + ":"
                        + request.getServerPort()
                        + request.getContextPath() + "/"
                        + "uploadfile/"+ShNo+"/"+"zj"+"/" + fileName;
                String path = request.getSession().getServletContext().getRealPath("/")
                        + "uploadfile/"  + "/";
                //上传文件名
                oldName = file[0].getOriginalFilename();
                File filepath = new File(path+ShNo+"/"+"zj"+"/" +fileName);
                //判断路径是否存在，如果不存在就创建一个
                if (!filepath.getParentFile().exists()) {
                    filepath.getParentFile().mkdirs();
                }
                //将上传文件保存到一个目标文件当中
                file[0].transferTo(new File(path +ShNo+"/"+"zj"+"/" + fileName));
                Map<String, Object> param = new HashMap<>();
                param.put("ShNo", ShNo);
                param.put("Type", Type);
                param.put("fileurl", fpath);
                param.put("newfilename", fileName);
                String url = BACKURL+"/uploadZjReport";
                Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
                String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
                return data;
            } else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Description 上传管理图纸及相关说明图片
     * @Author zhangxue
     * @Date 18:08 2018/7/12
     * @Param
     * @retrun
     **/
    @ResponseBody
    @RequestMapping(value = "/uploadManageImg")
    public String uploadManageImg(@RequestParam(value = "file", required = true) MultipartFile[] file,@RequestParam(value="ShNo")String ShNo,@RequestParam(value="Type")String Type,HttpServletRequest request) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String oldName = "";
        String fname = "";
        String fpath = "";
        try {
            if(!file[0].isEmpty()) {
                String fileExt = file[0]
                        .getOriginalFilename()
                        .substring(
                                file[0].getOriginalFilename()
                                        .lastIndexOf(".") + 1)
                        .toLowerCase();
                String fileName = UUID.randomUUID().toString()
                        .replaceAll("-", "")
                        + "_" + ShNo + "." + fileExt;
                fpath = request.getScheme() + "://"
                        + request.getServerName() + ":"
                        + request.getServerPort()
                        + request.getContextPath() + "/"
                        + "uploadfile/"+ShNo+"/"+"manimg"+"/" + fileName;
                String path = request.getSession().getServletContext().getRealPath("/")
                        + "uploadfile/"  + "/";
                //上传文件名
                oldName = file[0].getOriginalFilename();
                File filepath = new File(path+ShNo+"/"+"manimg"+"/" +fileName);
                //判断路径是否存在，如果不存在就创建一个
                if (!filepath.getParentFile().exists()) {
                    filepath.getParentFile().mkdirs();
                }
                //将上传文件保存到一个目标文件当中
                file[0].transferTo(new File(path +ShNo+"/"+"manimg"+"/" + fileName));
                Map<String, Object> param = new HashMap<>();
                param.put("ShNo", ShNo);
                param.put("Type", Type);
                param.put("fileurl", fpath);
                param.put("newfilename", fileName);
                String url = BACKURL+"/uploadManageImg";
                Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
                String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
                return data;
            } else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Description 上传商标注册图片
     * @Author zhangxue
     * @Date 18:08 2018/7/12
     * @Param
     * @retrun
     **/
    @ResponseBody
    @RequestMapping(value = "/uploadLogo")
    public String uploadLogo(@RequestParam(value = "file", required = true) MultipartFile[] file,@RequestParam(value="ShNo")String ShNo,@RequestParam(value="Type")String Type,HttpServletRequest request) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String oldName = "";
        String fname = "";
        String fpath = "";
        try {
            if(!file[0].isEmpty()) {
                String fileExt = file[0]
                        .getOriginalFilename()
                        .substring(
                                file[0].getOriginalFilename()
                                        .lastIndexOf(".") + 1)
                        .toLowerCase();
                String fileName = UUID.randomUUID().toString()
                        .replaceAll("-", "")
                        + "_" + ShNo + "." + fileExt;
                fpath = request.getScheme() + "://"
                        + request.getServerName() + ":"
                        + request.getServerPort()
                        + request.getContextPath() + "/"
                        + "uploadfile/"+ShNo+"/"+"logo"+"/" + fileName;
                String path = request.getSession().getServletContext().getRealPath("/")
                        + "uploadfile/"  + "/";
                //上传文件名
                oldName = file[0].getOriginalFilename();
                File filepath = new File(path+ShNo+"/"+"logo"+"/" +fileName);
                //判断路径是否存在，如果不存在就创建一个
                if (!filepath.getParentFile().exists()) {
                    filepath.getParentFile().mkdirs();
                }
                //将上传文件保存到一个目标文件当中
                file[0].transferTo(new File(path +ShNo+"/"+"logo"+"/" + fileName));
                Map<String, Object> param = new HashMap<>();
                param.put("ShNo", ShNo);
                param.put("Type", Type);
                param.put("fileurl", fpath);
                param.put("newfilename", fileName);
                String url = BACKURL+"/uploadLogo";
                Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
                String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
                return data;
            } else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Description 上传生产许可证
     * @Author zhangxue
     * @Date 18:08 2018/7/12
     * @Param
     * @retrun
     **/
    @ResponseBody
    @RequestMapping(value = "/uploadProductPermise")
    public String uploadProductPermise(@RequestParam(value = "file", required = true) MultipartFile[] file,@RequestParam(value="ShNo")String ShNo,@RequestParam(value="Type")String Type,HttpServletRequest request) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String oldName = "";
        String fname = "";
        String fpath = "";
        try {
            if(!file[0].isEmpty()) {
                String fileExt = file[0]
                        .getOriginalFilename()
                        .substring(
                                file[0].getOriginalFilename()
                                        .lastIndexOf(".") + 1)
                        .toLowerCase();
                String fileName = UUID.randomUUID().toString()
                        .replaceAll("-", "")
                        + "_" + ShNo + "." + fileExt;
                fpath = request.getScheme() + "://"
                        + request.getServerName() + ":"
                        + request.getServerPort()
                        + request.getContextPath() + "/"
                        + "uploadfile/"+ShNo+"/"+"proper"+"/" + fileName;
                String path = request.getSession().getServletContext().getRealPath("/")
                        + "uploadfile/"  + "/";
                //上传文件名
                oldName = file[0].getOriginalFilename();
                File filepath = new File(path+ShNo+"/"+"proper"+"/" +fileName);
                //判断路径是否存在，如果不存在就创建一个
                if (!filepath.getParentFile().exists()) {
                    filepath.getParentFile().mkdirs();
                }
                //将上传文件保存到一个目标文件当中
                file[0].transferTo(new File(path +ShNo+"/"+"proper"+"/" + fileName));
                Map<String, Object> param = new HashMap<>();
                param.put("ShNo", ShNo);
                param.put("Type", Type);
                param.put("fileurl", fpath);
                param.put("newfilename", fileName);
                String url = BACKURL+"/uploadProductPermise";
                Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
                String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
                return data;
            } else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Description 上传产品使用说明书
     * @Author zhangxue
     * @Date 18:08 2018/7/12
     * @Param
     * @retrun
     **/
    @ResponseBody
    @RequestMapping(value = "/uploadUseExplain")
    public String uploadUseExplain(@RequestParam(value = "file", required = true) MultipartFile[] file,@RequestParam(value="ShNo")String ShNo,@RequestParam(value="Type")String Type,HttpServletRequest request) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String oldName = "";
        String fname = "";
        String fpath = "";
        try {
            if(!file[0].isEmpty()) {
                String fileExt = file[0]
                        .getOriginalFilename()
                        .substring(
                                file[0].getOriginalFilename()
                                        .lastIndexOf(".") + 1)
                        .toLowerCase();
                String fileName = UUID.randomUUID().toString()
                        .replaceAll("-", "")
                        + "_" + ShNo + "." + fileExt;
                fpath = request.getScheme() + "://"
                        + request.getServerName() + ":"
                        + request.getServerPort()
                        + request.getContextPath() + "/"
                        + "uploadfile/"+ShNo+"/"+"userEx"+"/" + fileName;
                String path = request.getSession().getServletContext().getRealPath("/")
                        + "uploadfile/"  + "/";
                //上传文件名
                oldName = file[0].getOriginalFilename();
                File filepath = new File(path+ShNo+"/"+"userEx"+"/" +fileName);
                //判断路径是否存在，如果不存在就创建一个
                if (!filepath.getParentFile().exists()) {
                    filepath.getParentFile().mkdirs();
                }
                //将上传文件保存到一个目标文件当中
                file[0].transferTo(new File(path +ShNo+"/"+"userEx"+"/" + fileName));
                Map<String, Object> param = new HashMap<>();
                param.put("ShNo", ShNo);
                param.put("Type", Type);
                param.put("fileurl", fpath);
                param.put("newfilename", fileName);
                String url = BACKURL+"/uploadUseExplain";
                Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
                String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
                return data;
            } else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Description 上传产品图册
     * @Author zhangxue
     * @Date 18:08 2018/7/12
     * @Param
     * @retrun
     **/
    @ResponseBody
    @RequestMapping(value = "/uploadProductPhoto")
    public String uploadProductPhoto(@RequestParam(value = "file", required = true) MultipartFile[] file,@RequestParam(value="ShNo")String ShNo,@RequestParam(value="Type")String Type,HttpServletRequest request) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String oldName = "";
        String fname = "";
        String fpath = "";
        try {
            if(!file[0].isEmpty()) {
                String fileExt = file[0]
                        .getOriginalFilename()
                        .substring(
                                file[0].getOriginalFilename()
                                        .lastIndexOf(".") + 1)
                        .toLowerCase();
                String fileName = UUID.randomUUID().toString()
                        .replaceAll("-", "")
                        + "_" + ShNo + "." + fileExt;
                fpath = request.getScheme() + "://"
                        + request.getServerName() + ":"
                        + request.getServerPort()
                        + request.getContextPath() + "/"
                        + "uploadfile/"+ShNo+"/"+"proPhoto"+"/" + fileName;
                String path = request.getSession().getServletContext().getRealPath("/")
                        + "uploadfile/"  + "/";
                //上传文件名
                oldName = file[0].getOriginalFilename();
                File filepath = new File(path+ShNo+"/"+"proPhoto"+"/" +fileName);
                //判断路径是否存在，如果不存在就创建一个
                if (!filepath.getParentFile().exists()) {
                    filepath.getParentFile().mkdirs();
                }
                //将上传文件保存到一个目标文件当中
                file[0].transferTo(new File(path +ShNo+"/"+"proPhoto"+"/" + fileName));
                Map<String, Object> param = new HashMap<>();
                param.put("ShNo", ShNo);
                param.put("Type", Type);
                param.put("fileurl", fpath);
                param.put("newfilename", fileName);
                String url = BACKURL+"/uploadProductPhoto";
                Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
                String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
                return data;
            } else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Description 上传产品图册
     * @Author zhangxue
     * @Date 18:08 2018/7/12
     * @Param
     * @retrun
     **/
    @ResponseBody
    @RequestMapping(value = "/uploadHonorBook")
    public String uploadHonorBook(@RequestParam(value = "file", required = true) MultipartFile[] file,@RequestParam(value="ShNo")String ShNo,@RequestParam(value="Type")String Type,HttpServletRequest request) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String oldName = "";
        String fname = "";
        String fpath = "";
        try {
            if(!file[0].isEmpty()) {
                String fileExt = file[0]
                        .getOriginalFilename()
                        .substring(
                                file[0].getOriginalFilename()
                                        .lastIndexOf(".") + 1)
                        .toLowerCase();
                String fileName = UUID.randomUUID().toString()
                        .replaceAll("-", "")
                        + "_" + ShNo + "." + fileExt;
                fpath = request.getScheme() + "://"
                        + request.getServerName() + ":"
                        + request.getServerPort()
                        + request.getContextPath() + "/"
                        + "uploadfile/"+ShNo+"/"+"honor"+"/" + fileName;
                String path = request.getSession().getServletContext().getRealPath("/")
                        + "uploadfile/"  + "/";
                //上传文件名
                oldName = file[0].getOriginalFilename();
                File filepath = new File(path+ShNo+"/"+"honor"+"/" +fileName);
                //判断路径是否存在，如果不存在就创建一个
                if (!filepath.getParentFile().exists()) {
                    filepath.getParentFile().mkdirs();
                }
                //将上传文件保存到一个目标文件当中
                file[0].transferTo(new File(path +ShNo+"/"+"honor"+"/" + fileName));
                Map<String, Object> param = new HashMap<>();
                param.put("ShNo", ShNo);
                param.put("Type", Type);
                param.put("fileurl", fpath);
                param.put("newfilename", fileName);
                String url = BACKURL+"/uploadHonorBook";
                Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
                String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
                return data;
            } else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
         * @Description 编辑商户图片其他注意事项
         * @Author zhangxue
         * @Date 9:16 2018/9/20
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/saveOtherInfo",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveOtherInfo(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/saveOtherInfo";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
         * @Description 删除不同类型图片
         * @Author zhangxue
         * @Date 10:12 2018/9/13
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/delSHImage",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delSHImage(@RequestBody Map<String,Object> param,HttpServletRequest request){
        String path = request.getSession().getServletContext().getRealPath("/")
                + "uploadfile/";
        String ShNo = param.get("ShNo")!=null?param.get("ShNo").toString():null;
        String Name = param.get("Name")!=null?param.get("Name").toString():null;
        String Type = param.get("Type")!=null?param.get("Type").toString():null;
        String FilePath = "";
        if("授权书".equals(Type)){
            FilePath = path+ShNo+"/sqs/"+Name;
        }else if("执照".equals(Type)){
            FilePath = path+ShNo+"/zz/"+Name;
        }else if("身份证".equals(Type)){
            FilePath = path+ShNo+"/card/"+Name;
        }else if("质检报告".equals(Type)){
            FilePath = path+ShNo+"/zj/"+Name;
        }else if("管理图纸".equals(Type)){
            FilePath = path+ShNo+"/manimg/"+Name;
        }else if("商标注册".equals(Type)){
            FilePath = path+ShNo+"/logo/"+Name;
        }else if("商标注册".equals(Type)){
            FilePath = path+ShNo+"/logo/"+Name;
        }else if("生产许可证".equals(Type)){
            FilePath = path+ShNo+"/proper/"+Name;
        }else if("产品使用说明书".equals(Type)){
            FilePath = path+ShNo+"/userEx/"+Name;
        }else if("产品图册".equals(Type)){
            FilePath = path+ShNo+"/proPhoto/"+Name;
        }else if("荣誉证书".equals(Type)){
            FilePath = path+ShNo+"/honor/"+Name;
        }
        File delFile = new File(FilePath);

        if(delFile.exists()){
            Boolean flag = fileUtil.deleteFile(FilePath);
            if(flag){
                System.out.print("商户编号为"+ShNo+"的商户下名为"+Name+"的图片删除成功");
            }
        }
        String url = BACKURL+"/delSHImage";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }


    /**
         * @Description 查看商户详情
         * @Author zhangxue
         * @Date 16:14 2018/7/13
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/initLookInfo",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> initLookInfo(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/initLookInfo";
        Map<String,Object> resultMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
//        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return resultMap;
    }


    /*####################################################################################*/
    /*##############################   展位管理    #######################################*/
    /*####################################################################################*/
    /**
     * @Description 初始化展位编辑界面
     * @Author zhangxue
     * @Date 16:14 2018/7/13
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/initZWEditPage",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> initZWEditPage(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/initZWEditPage";
        Map<String,Object> resultMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
//        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return resultMap;
    }

    /**
     * @Description 查询展位品牌下拉
     * @Author zhangxue
     * @Date 16:14 2018/7/13
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryBrandJson",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> queryBrandJson(){
        String url = BACKURL+"/queryBrandJson";
        Map<String,Object> resultMap = SessionAndGetRemote.getRemoteResult(null,url,okHttpUtils);
        return resultMap;
    }

    /**
         * @Description 新增展位信息
         * @Author zhangxue
         * @Date 15:08 2018/7/13
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/saveZWInfo",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveZWInfo(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/saveZWInfo";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
         * @Description 删除展位信息
         * @Author zhangxue
         * @Date 16:59 2018/7/13
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/delZWInfo",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delZWInfo(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/delZWInfo";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
     * @Description 编辑展位信息
     * @Author zhangxue
     * @Date 16:59 2018/7/13
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/editZWInfo",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String editZWInfo(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/editZWInfo";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
     * @Description 查询所有展位信息
     * @Author zhangxue
     * @Date 12:45 2018/5/31
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryAllZWInfos",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllZWInfos(@RequestBody Map<String,Object> param,HttpSession session){
        String url = BACKURL+"/queryAllZWInfos";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /*####################################################################################*/
    /*##############################   品牌管理    #######################################*/
    /*####################################################################################*/
    /**
         * @Description 查询所有品牌名称
         * @Author zhangxue
         * @Date 17:18 2018/9/7
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryBrandList",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryBrandList(@RequestBody Map<String,Object> param,HttpSession session){
        String url = BACKURL+"/queryBrandList";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
     * @Description 通过品牌名称查询所有系列
     * @Author zhangxue
     * @Date 17:18 2018/9/7
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/querySeriesByBrandName",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String querySeriesByBrandName(@RequestBody Map<String,Object> param,HttpSession session){
        String url = BACKURL+"/querySeriesByBrandName";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
         * @Description 新增品牌和品牌系列
         * @Author zhangxue
         * @Date 10:41 2018/9/10
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/saveBrandAndSeries",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveBrandAndSeries(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/saveBrandAndSeries";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }
}
